package org.grayrabbit.cms.interceptor;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.grayrabbit.cms.action.common.AuthCheckAction;
import org.grayrabbit.cms.constant.ConstantWebApp;
import org.grayrabbit.cms.constant.UsercenterStaticValues;
import org.grayrabbit.cms.security.ActionPermission;
import org.grayrabbit.cms.security.PermissionChecker;
import org.grayrabbit.cms.security.SecurityUser;
import org.grayrabbit.cms.vo.system.OperCodeCheckBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor{
 
	private static final long serialVersionUID = 1L;
	private PermissionChecker permissionChecker = new ActionPermission();
	
	@Override
	public String intercept(ActionInvocation actioninvocation) throws Exception {
		String uri = getUri(actioninvocation.getProxy());
		Object ac =  actioninvocation.getAction(); 
		Map<String,Object> params =null;
		List<OperCodeCheckBean> list=null;
		if( ac instanceof AuthCheckAction){
			params = ActionContext.getContext().getParameters();
			Map<String ,List<OperCodeCheckBean>> map = ((AuthCheckAction) ac).loadAuthCheckMapInfo(); 
			list = map.get(actioninvocation.getProxy().getMethod());
		} 
		SecurityUser user = null;
		if(isFront(uri)){
			user =(SecurityUser) actioninvocation.getInvocationContext().getSession().get(UsercenterStaticValues.CURRENT_MEMBER);
		}else{
			user =(SecurityUser) actioninvocation.getInvocationContext().getSession().get(ConstantWebApp.CURRENT_ADMIN);
		}
		if(permissionChecker.check(user, uri, PermissionChecker.TARGET_TYPE_URI,params,list))
			return actioninvocation.invoke();
		else{
			if(user==null){
				if(isFront(uri)){
					
					String arg = ServletActionContext.getRequest().getQueryString();
					String fromUrl = null;
					if(StringUtils.isNotBlank(arg)){
						fromUrl = ServletActionContext.getRequest().getRequestURI()+"?"+arg;
					}
					else{
						fromUrl = ServletActionContext.getRequest().getRequestURI();
					}
					ServletActionContext.getRequest().setAttribute("fromUrl", fromUrl);
					return "front_login_page";
				}else{
					return "login_page";
				}
			}else{
				if(isFront(uri)){
					String fromUrl = null;
					String arg = ServletActionContext.getRequest().getQueryString();
					if(StringUtils.isNotBlank(arg)){
						fromUrl = ServletActionContext.getRequest().getRequestURI()+"?"+arg;
					}
					else{
						fromUrl = ServletActionContext.getRequest().getRequestURI();
					}
					ServletActionContext.getRequest().setAttribute("fromUrl", fromUrl);
					return "front_login_page";
				}else{
					return "login_page";
				}
			}
		}
	}

	private String getUri(ActionProxy proxy){
			return (StringUtils.isBlank(proxy.getNamespace())||"/".equals(proxy.getNamespace())?"":proxy.getNamespace()+"_")+proxy.getActionName()+"_"+proxy.getMethod();
	}
	
	private boolean isFront(String uri){
		if(uri.indexOf("/front/")>-1)
			return true;
		return false;
	}

}
